package com.ciwong.epaper.modules.epaper.util;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.adapter.MultipleServicesAdapter;
import com.ciwong.epaper.modules.me.bean.CheckValidServiceBean;
import com.ciwong.epaper.util.DeviceUtil;
import com.ciwong.epaper.util.DisplayUtils;

import java.util.List;

/**
 * Created by yyf on 2017/8/21.
 */
public class MultipleServicesDialog extends Dialog implements AdapterView.OnItemClickListener {

    private List<CheckValidServiceBean> checkValidServiceBeanList;
    private TextView tv_title;
    private ListView nll_services;
    private MultipleServicesAdapter multipleServicesAdapter;


    public MultipleServicesDialog(Context context, List<CheckValidServiceBean> checkValidServiceBeanList, OnServicesItemClick onServicesItemClick) {
        super(context, R.style.round_corner_dialog);
        this.onServicesItemClick = onServicesItemClick;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.checkValidServiceBeanList = checkValidServiceBeanList;
        LayoutInflater from = LayoutInflater.from(context);
        View multiple_services_view = from.inflate(R.layout.multiple_services_dialog, null);



        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DeviceUtil.getDeviceWidth(context) - DisplayUtils.dip2px(context, 50), LinearLayout.LayoutParams.WRAP_CONTENT);
        addContentView(multiple_services_view, params);

        init();
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }


    private void init() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        nll_services =  findViewById(R.id.nll_services);
        multipleServicesAdapter = new MultipleServicesAdapter(checkValidServiceBeanList);
        nll_services.setOnItemClickListener(this);
        nll_services.setAdapter(multipleServicesAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (onServicesItemClick != null) {
            onServicesItemClick.onServicesItemClick(position);
        }
        if(isShowing()){
            dismiss();
        }
    }

    private OnServicesItemClick onServicesItemClick;

    public interface OnServicesItemClick {
        void onServicesItemClick(int position);
    }
}

